package app.auto.runner;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;

import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import app.auto.runner.base.ActivityPool;
import app.auto.runner.base.ActivityStack;
import app.auto.runner.base.action.ViewInflater;
import app.auto.runner.base.framework.CrashHandler;
import app.auto.runner.base.utility.BackStack;
import app.auto.runner.base.utility.OrientUtil;
import app.auto.runner.base.utility.ToastUtil;

/**
 * Created by Administrator on 2017/11/3.
 */
public class ActivityBase extends Activity {

    public Activity getActvity() {
        return this;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        if (backstackOption)
//            backStack.registerKeyInActivity(true);
        OrientUtil.orient(this);
        ActivityStack.getInstance().addActivity(this);
        ActivityPool.currAty = this;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            BackStack backStack = BackStack.map(this);
            if (backStack != null) {
                if (backStack.backPressed()) {
                    return false;
                }
            }
        }
        if (event == null) {
            return false;
        } else {
            try {
                return super.onKeyDown(keyCode, event);
            } catch (Exception e) {
                CrashHandler.errorDialog(e);
            }
            return false;
        }

    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(new ViewInflater(this).inflate(layoutResID, null));
    }

    ToastUtil toastUtil = new ToastUtil(this);

    public void showToast(String string) {
        toastUtil.toast(string, "");
    }

    public String getTextString(int id) {
        return ((TextView) findViewById(id)).getText().toString();
    }
    public String getTextString(int... id) {
        View v = getView(id);
        return ((TextView) v).getText().toString();
    }

    @Nullable
    public View getView(int... id) {
        View v = null;
        for (int i : id) {
            if (v == null)
                v = findViewById(i);
            else {
                v = v.findViewById(i);
            }
        }
        return v;
    }

    public void setTextString(String value,int... id) {
        View v = getView(id);
        ((TextView) v).setText(value);
    }
    public TextView getTextView(int id) {
        return (TextView) findViewById(id);
    }

    public void setTextString(int id, String t) {
        ((TextView) findViewById(id)).setText(t);
    }

    public EditText getEditText(int id) {
        return (EditText) findViewById(id);
    }

    public RadioButton getRadioButton(int id) {
        return (RadioButton) findViewById(id);
    }

    public ImageView getImageView(int id) {
        return (ImageView) findViewById(id);
    }

    public View getView(int id) {
        return findViewById(id);
    }

    public void click(int id, View.OnClickListener onClickListener) {
        getView(id).setOnClickListener(onClickListener);
    }


    public void setTextString(View view, int id, String t) {
        ((TextView) view.findViewById(id)).setText(t);
    }

    public ViewGroup getGroup(int id) {
        return (ViewGroup) findViewById(id);
    }

}
